<template>
  <a-modal
    class="fillmaskModal"
    :title="title"
    :width="1200"
    :visible="visible"
    :confirmLoading="confirmLoading"
:ok-button-props="{ style: { display: 'none' } }"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top: 5%; height: 85%; overflow-y: hidden"
  >
    <a-row style="padding: 0px 20px" v-if="!!taskInfo">
      <p class="title">任务信息</p>

      <a-col :span="12" style="margin-bottom: 16px" class="item">
        任务名称：{{ taskInfo.taskName }}
      </a-col>
      <a-col :span="12" style="margin-bottom: 16px" class="item">
        核算标准：{{ taskInfo.standardName }}
      </a-col>
      <a-col :span="12" style="margin-bottom: 16px" class="item">
        填报周期：{{ taskInfo.fillingTime.replace(",", "至") }}
      </a-col>
      <a-col :span="12" style="margin-bottom: 16px" class="item">
        创 建 人 ：{{ taskInfo.createBy }}
      </a-col>
      <a-col :span="12" style="margin-bottom: 16px" class="item">
        创建时间：{{ taskInfo.createTime }}
      </a-col>

      <a-col :span="24" style="margin-bottom: 16px">
        <p class="title">碳排放量</p>
        <a-table
          :columns="columns"
          :data-source="emission"
          :pagination="false"
        ></a-table>
      </a-col>
      <a-col
        :span="24"
        style="margin-bottom: 16px"
        v-for="(item, index) in facilitiesBoundaryList"
        :key="index"
      >
        <p class="title">{{ item.facilitiesName }}-{{ item.boundaryName }}</p>
        <a-table
          :pagination="false"
          :columns="item.header"
          :data-source="item.data"
          size="small"
        />
      </a-col>
    </a-row>
    <a-spin :spinning="confirmLoading">
      <!-- {{ facilitiesList }} -->
    </a-spin>
  </a-modal>
</template>

<script>
import { queryTaskBoundaryListByTaskId, auditInformant } from "@/api/api.js";
import {
  queryFillingTask,
  addRole,
  editRole,
  duplicateCheck,
  updateTaskBoundary,
} from "@/api/api";
const columns = [
  {
    dataIndex: "boundaryName",
    title: "源类别",
    key: "boundaryName",
    scopedSlots: { customRender: "boundaryName" },
  },
  {
    title: "排放量 单位：tCO₂e",
    dataIndex: "emission",
    key: "emission",
  },
];
export default {
  name: "RoleModal",
  data() {
    return {
      columns,
      taskInfo: null,
      emission: null,
      facilitiesBoundaryList: null,
      facilitiesList: null,
      normFile: null,
      title: "操作",
      visible: false,
      roleDisabled: false,
      model: {},
      layout: {
        labelCol: { span: 4 },
        wrapperCol: { span: 12 },
      },
      confirmLoading: false,
      validatorRules: {
        consume: [{ required: true, message: "请填写消耗量!" }],
        roleCode: [
          { required: true, message: "请输入角色名称!" },
          { min: 0, max: 64, message: "长度不超过 64 个字符", trigger: "blur" },
          { validator: this.validateRoleCode },
        ],
        description: [
          {
            min: 0,
            max: 126,
            message: "长度不超过 126 个字符",
            trigger: "blur",
          },
        ],
      },
    };
  },
  components: {},
  created() {
    //备份model原始值
    this.modelDefault = JSON.parse(JSON.stringify(this.model));
  },
  methods: {
    add() {
      this.edit(this.modelDefault);
    },
    info(record) {
      //   console.log(record);
      queryFillingTask({
        fillingId: record.fillingId,
        taskId: record.taskId,
      }).then((res) => {
        //   console.log(res);
        this.model = res.result;
        this.visible = true;
      });
    },
    info(record) {
      // this.model = Object.assign({}, record);
      console.log(record);
      this.visible = true;
      //编辑页面禁止修改角色编码
      // if (this.model.id) {
      //   this.roleDisabled = true;
      // } else {
      //   this.roleDisabled = false;
      // }
      let param = {
        businessKey: record.busiFormId,
      };
      auditInformant(param).then((res) => {
        // this.facilitiesList = res.result;
        console.log(res);
        if (res.result) {
          this.taskInfo = res.result.taskInfo;
          this.emission = res.result.emissionStatList;
          this.facilitiesBoundaryList = res.result.facilitiesBoundaryList;
        }
        // 数据分类
      });
    },
    edit(record) {
      // this.model = Object.assign({}, record);
      console.log(record);
      this.visible = true;
      //编辑页面禁止修改角色编码
      // if (this.model.id) {
      //   this.roleDisabled = true;
      // } else {
      //   this.roleDisabled = false;
      // }
      let param = {
        businessKey: record.busiFormId,
      };
      auditInformant(param).then((res) => {
        // this.facilitiesList = res.result;
        console.log(res);
        if (res.result) {
          this.taskInfo = res.result.taskInfo;
          this.emission = res.result.emissionStatList;
          this.facilitiesBoundaryList = res.result.facilitiesBoundaryList;
        }
        // 数据分类
      });
    },
    close() {
      this.$emit("close");
      this.visible = false;
    },
    handleOk() {
      const that = this;
      //   console.log(this.model);

      var param = {
        description: "",
        filePath: "",
        id: this.model.fillingId,
        jsonInfo: JSON.stringify(this.model.fieldList),
      };
      // updateTaskBoundary(param).then(res =>{
      //   //   console.log(res)
      //     if (res.success) {
      //     that.$message.success(res.message);
      //     that.$emit("ok");
      //      this.close();
      //   } else {
      //     that.$message.warning(res.message);
      //      this.close();
      //   }
      // }).finally(() => {
      //   that.confirmLoading = false;
      //   that.close();
      // });

      // 触发表单验证
      this.$refs.form.validate((valid) => {
        if (valid) {
          that.confirmLoading = true;
          let obj;
          obj = updateTaskBoundary(param);
          // if (!this.model.id) {
          //  obj =   updateTaskBoundary(this.model)
          //   // obj = addRole(this.model);
          // } else {
          //   // obj = editRole(this.model);
          // }

          obj
            .then((res) => {
              if (res.success) {
                that.$message.success(res.message);
                that.$emit("ok");
              } else {
                that.$message.warning(res.message);
              }
            })
            .finally(() => {
              that.confirmLoading = false;
              that.close();
            });
        } else {
          return false;
        }
      });
    },
    handleCancel() {
      this.close();
    },
    validateRoleCode(rule, value, callback) {
      if (/[\u4E00-\u9FA5]/g.test(value)) {
        callback("角色编码不可输入汉字!");
      } else {
        let params = {
          tableName: "sys_role",
          fieldName: "role_code",
          fieldVal: value,
          dataId: this.model.id,
        };
        duplicateCheck(params).then((res) => {
          if (res.success) {
            callback();
          } else {
            callback(res.message);
          }
        });
      }
    },
  },
};
</script>

<style lang="scss" >
@import "@/assets/css/newrModule.scss";
.fillmaskModal .ant-modal-body {
  padding-top: 0px !important;
  padding-left: 0;
  padding-right: 0;
}
.fillmaskModal .ant-spin-nested-loading {
  padding: 12px 24px !important;
}
.fillmaskModal .ant-select-selection-selected-value {
  font-size: 12px;
}
.fillmaskModal .info {
  background: #ebf5fe;
  background-image: linear-gradient(
    rgb(209, 231, 253),
    rgba(245, 249, 253, 0.644)
  );
  padding: 12px 24px !important;
  margin-bottom: 10px;
  //   border-bottom: 1px dashed rgb(241, 241, 241);
  p {
    font-size: 14px;
    font-weight: 600;
    margin: 0;
    margin-bottom: 4px;
  }
}
</style>